package com.hai.qtalk.base;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 * DAO层的基类
 * @author xinhai
 */
public class BaseDAO<T> {

	//读取xml配置文件并根据配置文件构造 会话工厂类 sessionFactory来进行会话管理
	private Configuration cfg = new Configuration().configure();
	private ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
			.applySettings(cfg.getProperties()).buildServiceRegistry();
	private SessionFactory sessionFactory = cfg
			.buildSessionFactory(serviceRegistry);
	private Session session = getSession();

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() {
		if (sessionFactory.getCurrentSession() == null) {
			return sessionFactory.openSession();
		} else
			return sessionFactory.getCurrentSession();
	}

	public long save(T object) {

		try {
			session = getSession();
			Transaction transaction = this.sessionFactory.getCurrentSession()
					.beginTransaction();
			session.save(object);
			/** LAST_INSERT_ID是不用查表的，而且只针对当前连接，也就是说别的连接的更新不会影响到当前连接的取值。*/
			BigInteger lastId = (BigInteger)getSession().createSQLQuery("select last_insert_id();").uniqueResult();
			transaction.commit();
			return lastId.longValue();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public void update(T object) {

		try {
			session = getSession();
			Transaction transaction = this.sessionFactory.getCurrentSession()
					.beginTransaction();
			session.update(object);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delete(T object) {

		try {
			session = getSession();
			Transaction transaction = this.sessionFactory.getCurrentSession()
					.beginTransaction();
			session.delete(object);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public T find(Class<? extends T> clazz, Serializable id) {

		try {
			session = getSession();
			Transaction transaction = this.sessionFactory.getCurrentSession()
					.beginTransaction();
			T t = (T) session.get(clazz, id);
			transaction.commit();
			return t;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<T> list(String hql) {
		try {
			session = getSession();
			Transaction transaction = this.sessionFactory.getCurrentSession()
					.beginTransaction();
			List<T> list = session.createQuery(hql).list();
			transaction.commit();
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	public T find(String hql) {
		try {
			session = getSession();
			Transaction transaction = this.sessionFactory.getCurrentSession()
					.beginTransaction();
			List<T> list = session.createQuery(hql).list();
			transaction.commit();
			if (list != null && list.size() > 0) {
				return list.get(0);
			} else {
				return null;
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
}